package lanou.autohome.forum.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import lanou.autohome.MySwipeRefreshLayout;
import lanou.autohome.R;
import lanou.autohome.basetool.BaseFragment;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.forum.adapters.ForumFeatureAllAdapter;
import lanou.autohome.forum.beans.ForumFeatureAllBean;

/**
 * Created by dllo on 16/8/18.
 */
public class ForumFeatureAllFragment extends BaseFragment {


    private ListView listview;
    private MySwipeRefreshLayout mySwipeRefreshLayout;



    @Override
    protected int setLayout() {
        return R.layout.fragment_forum_featured_all;
    }

    @Override
    protected void initView() {
        listview = bindVie(R.id.listview_forum_featured);
        mySwipeRefreshLayout = bindVie(R.id.myswiperefreshlayout_forum_featured_all);
        //上拉加载, 下拉刷新
        initReLo();

    }

    //上拉加载 下拉刷新
    private void initReLo() {
        //下拉刷新
        mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //清空所有数据
                listview.setAdapter(null);
                mySwipeRefreshLayout.setRefreshing(false);
//                Toast.makeText(mcontext, "正在刷新,请稍等...", Toast.LENGTH_SHORT).show();
                intiData();
                Toast.makeText(mcontext, "刷新成功!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ForumFeatureAllAdapter froumfeatureAlladapter;
    @Override
    protected void intiData() {
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        NetTool netTool = new NetTool();
        netTool.getNetData(url, ForumFeatureAllBean.class, new NetTool.NetListener<ForumFeatureAllBean>() {

            @Override
            public void onSuccess(ForumFeatureAllBean forumFeatureAllBean) {
                froumfeatureAlladapter = new ForumFeatureAllAdapter(getContext());
                froumfeatureAlladapter.setForumFeatureAllBean(forumFeatureAllBean);
                listview.setAdapter(froumfeatureAlladapter);
                //停止刷新
                mySwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });


        //添加点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mcontext, "point", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), ForumFeatureAllItemsActivity.class);
                int urlAdd = froumfeatureAlladapter.getUrlAdd();
                Log.d("ForumFeatureAllFragment", "发送的topPicId:" + urlAdd);
                intent.putExtra("toPicId", urlAdd);
//                startActivity(intent);
                getContext().startActivity(intent);
            }
        });
    }
}